﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web.Factories;
using CSP.Common.Factories;
using System.Configuration;

namespace CSP.Web.UserControls
{
    public partial class TopNavigation : System.Web.UI.UserControl
    {

        private string _currentPagePath;

        private string _currentPagePathWithParm;
        
        private int _uid;

        /// <summary>
        /// discuz路径
        /// </summary>
        private string _discuzUrl;

        /// <summary>
        /// 当前承载页面的路径
        /// </summary>
        public string CurrentPagePath
        {
            get
            {
                return _currentPagePath;
            }
        }

        /// <summary>
        /// 当前带参的地址
        /// </summary>
        public string CurrentPagePathWithParm
        {
            get
            {
                return _currentPagePathWithParm;
            }
        }

          /// <summary>
        /// Uid
        /// </summary>
        protected int Uid
        {
            get
            {
                return _uid;
            }
        }

        /// <summary>
        /// discuz url
        /// </summary>
        protected string DiscuzUrl
        {
            get
            {
                return _discuzUrl;
            }
        }

        /// <summary>
        /// 当程序未初始化控件时加载
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (this.Page.Request["userlogin"] != null)
            {
                UserLogin();
            }
            else if (this.Page.Request["loginout"] != null)
            {
                LoginOut();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentPagePath = this.Page.Request.Path;
            _discuzUrl = ConfigurationManager.AppSettings["DiscuzDomain.URL"].ToString();
            _uid = Convert.ToInt32(Session["Uid"]);
            if (Request["urlback"] == null)
            {
                _currentPagePathWithParm = Server.UrlEncode(this.Page.Request.Url.OriginalString);
            }
            else
            {
                _currentPagePathWithParm = Server.UrlEncode(Request["urlback"].ToString());
            }
            
        }

        /// <summary>
        /// 用户登陆状态验证
        /// </summary>
        private void UserLogin()
        {
            string userName = UserProviderFactory.GetInstance(this.Page).UserProvider.UserName;
            string returnInfo = "";
            //用户登陆状态验证失败
            if (string.IsNullOrWhiteSpace(userName))
            {
                returnInfo = "{\"Status\":0,\"UserName\":\"" + userName + "\"}";
            }
            else
            {
                returnInfo = "{\"Status\":1,\"UserName\":\"" + userName + "\"}";
            }
            Response.Clear();
            Response.Write(returnInfo);
            Response.End();
        }

        /// <summary>
        /// 用户退出
        /// </summary>
        private void LoginOut()
        {
            Response.ClearContent();
            UserProviderFactory.GetInstance(this.Page).UserProvider.LoginOut();
            string cookieName=CookieFactory.GetInstace().GetFullNameByName("AuthKey");
            Response.Write("{\"Status\":1,\"CookieName\":\"" + cookieName + "\"}");
            Response.End();
        }
    }
}